package com.aorise.service.bayonet.impl;

import com.aorise.exception.DataValidationException;
import com.aorise.exception.ServiceException;
import com.aorise.exception.SystemException;
import com.aorise.mapper.bayonet.BayonetMapper;
import com.aorise.model.bayonet.*;
import com.aorise.service.bayonet.BayonetService;
import com.aorise.utils.*;
import com.aorise.utils.excel.ExcelUtil;
import com.aorise.utils.excel.ImportExcel;
import com.aorise.utils.json.InnerResult;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.util.*;

import static com.aorise.utils.excel.ExcelUtil.getWorkbok;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/19
 * @Description:
 */
@Service
public class BayonetServiceImpl implements BayonetService {

    @Autowired
    BayonetMapper bayonetMapper;

    /**
     * 保存临时文件文件夹
     */
    private String directoryName = "excelTemp";

    /**
     * 添加卡口
     * @param bayonetModel
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public boolean addBayonet(BayonetModel bayonetModel) {
        Map map =Maps.newHashMap();
        map.put("state",ConstDefine.STATE_ABLE);
        map.put("baNumber",bayonetModel.getBaNumber());
        BayonetModel bayonet =bayonetMapper.selectBayonetByBaNumber(map);
        if(bayonet !=null){
            throw new SystemException("卡口编码已存在,无法保存");
        }
        boolean flag =false;
        Integer loginUserId = getLoginUser().getId();
        bayonetModel.setOnline(ConstDefine.CAMERA_ONLINE);
        bayonetModel.setState(ConstDefine.STATE_ABLE);
        bayonetModel.setCreateUser(loginUserId);
        bayonetModel.setEditUser(loginUserId);
        /** 添加卡口 */
        flag = getBoolean(bayonetMapper.addBayonet(bayonetModel));
        /**卡口ID*/
        Integer bayonetId = bayonetModel.getId();
        List<BayonetLaneModel> bayonetLanes = bayonetModel.getBayonetLane();
        if(CollectionUtils.isNotEmpty(bayonetLanes)) {
            bayonetLanes.forEach(l -> {
                l.setBayonetId(bayonetId);
                l.setState(ConstDefine.STATE_ABLE);
                l.setCreateUser(loginUserId);
                l.setEditUser(loginUserId);
            });
            flag =getBoolean(bayonetMapper.addBayonetLane(bayonetLanes));
        }
        return flag;
    }

    /**
     * 分页获取卡口列表
     * @param bayonetPageVO
     * @return
     */
    @Override
    public Page getBayonetByPage(BayonetPageVO bayonetPageVO) {
        Page page = new Page();
        String pageNum = bayonetPageVO.getPageNum();
        String pageSize = bayonetPageVO.getPageSize();
        pageSize = StringUtils.isBlank(pageSize) ? "10" : pageSize;
        pageNum = StringUtils.isBlank(pageNum) ? "1" : pageNum;
        page.setCurrentPage(Long.valueOf(pageNum));
        page.setEveryPage(Long.valueOf(pageSize));
        Map<String, Object> map = Maps.newHashMap();
        map.put("bayonet", bayonetPageVO);
        map.put("state", ConstDefine.STATE_ABLE);
        long count = bayonetMapper.selectBayonetCount(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                map.put("beginIndex", page.getBeginIndex());
                map.put("endinIndex", page.getEndinIndex());
                /**查询分页条数*/
                page.setList(bayonetMapper.selectBayonetList(map));
            }
        } else {
            page.setTotalCount(0L);
        }
        return page;
    }

    /**
     * 导出卡口信息
     * @param bayonetPageVO 入参条件
     * @param response
     */
    @Override
    public void exportBayonet(BayonetPageVO bayonetPageVO, HttpServletResponse response) throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        map.put("bayonet", bayonetPageVO);
        map.put("state", ConstDefine.STATE_ABLE);
        List<LinkedHashMap<String,String>> bayonets = bayonetMapper.getBayonetByMap(map);
        for(LinkedHashMap bayonet:bayonets){
            bayonet.put("baType",getChinese( bayonet.get("baType").toString(),"bayonetType"));
            bayonet.put("online",getChinese( bayonet.get("online").toString(),"online"));
            bayonet.put("laneType",getChinese( bayonet.get("laneType").toString(),"laneType"));
            bayonet.put("intoOut",getChinese( bayonet.get("intoOut").toString(),"intoOut"));
            bayonet.put("direction",getChinese( bayonet.get("direction").toString(),"direction"));
        }

        writeExcel(bayonets,3, "卡口信息", "bayonet.xlsx", response);
    }

    /**
     * 导入卡口信息
     * @param file 导入excel
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = { Exception.class })
    @Override
    public InnerResult<String> importBayonet(MultipartFile file) {
        ImportExcel excelReader = new ImportExcel();
        List<ArrayList<String>> dataList = Lists.newArrayList();
        String perPurpose = "";//下一个用途类型
        String purpose = "";//当前用途类型
        int code =0;
        List<BayonetModel> bayonets = Lists.newArrayList();
        BayonetModel bayonet =null;
        BayonetLaneModel bayonetLane =null;
        try {
            dataList = excelReader.readExcelContent(file.getInputStream());
            List<BayonetLaneModel> bayonetLanes =null;

            /**验证每列数据格式*/
            String errMsg = checkExcelData(dataList);
            if (!errMsg.equals("")) {
                /**数据格式有误*/
                return new InnerResult<String>(false, errMsg);
            }

            for(int i =0;i<dataList.size();i++){
//                当前数据
                List<String> date = dataList.get(i);
                purpose =date.get(0);
                if(StringUtils.isNotBlank(purpose)){
                    bayonetLanes =Lists.newArrayList();
                    bayonet= BayonetModel.builder().baNumber(date.get(0)).baName(date.get(1)).baType(getConstant(date.get(2),"bayonetType"))
                                    .group(date.get(3))
                            .laneSum(Integer.valueOf(date.get(4))).online(getConstant(date.get(5),"online"))
                            .longitude(date.get(6)).latitude(date.get(7)).build();
                }
                bayonetLane = BayonetLaneModel.builder().laneNo(Integer.valueOf(date.get(8))).laneType(getConstant(date.get(9), "laneType")).intoOut(getConstant(date.get(10), "intoOut"))
                        .direction(getConstant(date.get(11),"direction")).speed(date.get(12) ==""?null:Integer.valueOf(date.get(12)) )
                        .exeSpeed(date.get(13) ==""?null:Integer.valueOf(date.get(13)) ).build();

                //添加车道
                bayonetLanes.add(bayonetLane);
                if(StringUtils.isNotBlank(purpose)){
                    bayonets.add(bayonet);
                }
                //获取下一个用途
                perPurpose = dataList.get( i==dataList.size()-1?i:i+1 ).get(0);
                if( ((!purpose .equals(perPurpose)) && StringUtils.isNotBlank(perPurpose) ) || i ==dataList.size()-1){
                    if(i ==dataList.size()-1){
                        bayonets.get(code).setBayonetLane(bayonetLanes);
                    }else {
                        bayonets.get(code).setBayonetLane(bayonetLanes);
                    }
                    code ++;
                }

            }
            Map map = Maps.newHashMap();
            map.put("state", ConstDefine.STATE_ABLE);
            for( BayonetModel bay:bayonets){
                map.put("gName",bay.getGroup());
                Integer loginUserId = getLoginUser().getId();
                bay.setGroup(String.valueOf(bayonetMapper.selectGroupByGName(map)));
                bay.setState(ConstDefine.STATE_ABLE);
                bay.setCreateUser(loginUserId);
                bay.setEditUser(loginUserId);
                bayonetMapper.addBayonet(bay);
                setBayonetLandState(bay, loginUserId);
                bayonetMapper.addBayonetLane(bay.getBayonetLane());
            }
            return new InnerResult<String>(true, "成功导入"+ dataList.size() +"条数据!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataValidationException("请使用正规的模板进行插入数据");
        }
    }

    /**
     * 卡口详情
     * @param bayonetID 卡口ID
     * @return
     */
    @Override
    public BayonetInfo getBayonetInfo(Integer bayonetID) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("bayonetID", bayonetID);
        map.put("state", ConstDefine.STATE_ABLE);
        return bayonetMapper.getBayonetInfo(map);
    }

    /**
     * 修改卡口信息
     * @param bayonet   修改卡口信息入参
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = { Exception.class })
    @Override
    public boolean upBayonet(BayonetModel bayonet) {

        Map<String, Object> bayonetMap = Maps.newHashMap();
        bayonetMap.put("state", ConstDefine.STATE_ABLE);
        bayonetMap.put("baNumber",bayonet.getBaNumber());
        BayonetModel bayonetModel = bayonetMapper.selectBayonetByBaNumber(bayonetMap);
        if( bayonetModel !=null && bayonetModel.getId() != bayonet.getId()){
            throw new SystemException("卡口编码已存在,无法保存");
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("bayonetID", bayonet.getId());
        map.put("state", ConstDefine.STATE_DISABLE);
        bayonetMapper.upBayonetLane(map);
        if(CollectionUtils.isNotEmpty(bayonet.getBayonetLane())){
            Integer loginUserId = getLoginUser().getId();
            setBayonetLandState(bayonet, loginUserId);
            bayonetMapper.addBayonetLane(bayonet.getBayonetLane());
        }

        bayonet.setEditUser(getLoginUser().getId());
        map.put("state", ConstDefine.STATE_ABLE);
        return getBoolean(bayonetMapper.upBayonet(bayonet));
    }

    private void setBayonetLandState(BayonetModel bayonet, Integer loginUserId) {
        for(BayonetLaneModel lane:bayonet.getBayonetLane()){
            lane.setBayonetId(bayonet.getId());
            lane.setState(ConstDefine.STATE_ABLE);
            lane.setCreateUser(loginUserId);
            lane.setEditUser(loginUserId);
        }
    }

    /**
     * 获取卡口分组情况
     * @return
     */
    @Override
    public List<BayonetGroup> getBayonetGroup() {
        return bayonetMapper.getBayonetGroup();
    }

    /**
     * 通过分组获取一级分组下的卡口和二级分组下的卡口
     * @param groupId   分组ID
     * @return
     */
    @Override
    public List<BayonetVO> getBayonetByGroup(Integer groupId) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("groupId",groupId);
        map.put("state",ConstDefine.STATE_ABLE);
        return bayonetMapper.getBayonetByGroup(map);
    }


    /**
     * 获取对象属性
     * @param t
     * @return
     */
    private PropertyDescriptor[] getBeanInfo(Object t) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
            return beanInfo.getPropertyDescriptors();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 导出excel
     * @param bayonets      数据
     * @param initialRow    起始行
     * @param fileName      文件名
     * @param templateName  模版名
     * @param response
     */
    private void writeExcel(List<LinkedHashMap<String,String>> bayonets,int initialRow, String fileName, String templateName, HttpServletResponse response) {
        OutputStream out = null;
        String fileNameTemp = System.currentTimeMillis() + "Temp.xlsx";
        try {

            //判断文件夹存在，不存在创建
            File file = new File(directoryName);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdir();
            }

            ClassPathResource classPathResource = new ClassPathResource("template/" + templateName);
            InputStream is = classPathResource.getInputStream();
            boolean iRet = ExcelUtil.copy(is, directoryName + "\\" + fileNameTemp);
            if (!iRet) {
                throw new ServiceException("复制模板文件失败");
            }

            // 读取Excel文档
            File finalXlsxFile = new File(directoryName + "\\" + fileNameTemp);

            Workbook workBook = getWorkbok(finalXlsxFile);
            // sheet 对应一个工作页
            Sheet sheet = workBook.getSheetAt(0);


            /**
             * 往Excel中写新数据
             */
            List<List<String>> bayExcel = Lists.newArrayList();
            for (Map bayonet : bayonets) {
                List<String> strings = new ArrayList<String>(bayonet.values());
                bayExcel.add(strings);
            }

            initialRow = initialRow - 1;
            String perPurpose = "";//上一个用途类型
            String purpose = "";//当前用途类型
//          从第initialRow行开始
            int row = initialRow;
            for(int i =0;i<bayExcel.size();i++){
                //创建一行
                Row row1 = sheet.createRow(row++);
                purpose = String.valueOf(bayExcel.get(i).get(0));

                for (int j = 0; j < bayExcel.get(i).size(); j++) {
                    //写入列数据
                    if (j == 8) {
                        row1.createCell(j).setCellValue(String.valueOf(bayExcel.get(i).get(j )));
                    } else if (j > 8) {
                        row1.createCell(j).setCellValue(String.valueOf(bayExcel.get(i).get(j )));
                    } else {
                        if ((!(purpose.equals(perPurpose)))) {
                            row1.createCell(j).setCellValue(String.valueOf(bayExcel.get(i).get(j)));
                        }
                    }
                }
                perPurpose = purpose;
            }
            // 创建文件输出流，准备输出电子表格：这个必须有，否则你在sheet上做的任何操作都不会有效
            out = new FileOutputStream(directoryName + "\\" + fileNameTemp);
            workBook.write(out);
            //下载文件
            ExcelUtil.downloadFile(response, directoryName + "\\" + fileNameTemp, fileName, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 获取常量值的中文
     *
     * @param num  常量值
     * @param type 常量值类型
     * @return
     */
    private String getChinese(String num, String type) {
        switch (type) {
            case "bayonetType":
                for (BayonetTypeEnum bay : BayonetTypeEnum.values()) {
                    if (num .equals( bay.getCode().toString() )) {
                        return bay.getChinese();
                    }
                }
            case "online":
                if (num .equals( String.valueOf(ConstDefine.CAMERA_ONLINE) )) {
                    return "在线";
                } else if (num .equals( ConstDefine.CAMERA_OFFLINE) ) {
                    return "离线";
                }
            case "laneType":
                for (LaneTypeEnum lan : LaneTypeEnum.values()) {
                    if (num .equals( lan.getCode().toString() )) {
                        return lan.getChinese();
                    }
                }
            case "intoOut":
                for (IntoOutEnum into : IntoOutEnum.values()) {
                    if (num .equals( into.getCode().toString()) ) {
                        return into.getChinese();
                    }
                }
            case "direction":
                for (DirectionEnum direction : DirectionEnum.values()) {
                    if (num .equals( direction.getCode().toString()) ) {
                        return direction.getChinese();
                    }
                }
        }
        return "";
    }

    /**
     * 获取常量值
     * @param ch
     * @param type
     * @return
     */
    private Integer getConstant(String ch, String type) {
        switch (type) {
            case "bayonetType":
                for (BayonetTypeEnum bay : BayonetTypeEnum.values()) {
                    if (ch.equals(bay.getChinese())) {
                        return bay.getCode();
                    }
                }
            case "online":
                if (ch.equals("离线")) {
                    return ConstDefine.CAMERA_OFFLINE;
                }else {
                    return ConstDefine.CAMERA_ONLINE;
                }
//                if (ch.equals("在线")){
//                    return ConstDefine.CAMERA_ONLINE;
//                } else if (ch.equals("离线")) {
//                    return ConstDefine.CAMERA_OFFLINE;
//                }
            case "laneType":
                for (LaneTypeEnum lan : LaneTypeEnum.values()) {
                    if (ch.equals(lan.getChinese())) {
                        return lan.getCode();
                    }
                }
            case "intoOut":
                for (IntoOutEnum into : IntoOutEnum.values()) {
                    if (ch.equals(into.getChinese())) {
                        return into.getCode();
                    }
                }
            case "direction":
                for (DirectionEnum direction : DirectionEnum.values()) {
                    if (ch.equals(direction.getChinese())) {
                        return direction.getCode();
                    }
                }
        }
        return null;
    }

    private String checkExcelData(List<ArrayList<String>> dataList){
        /**数据效验*/
        DataValidation dataValidation = new DataValidation();
        List<String> errStr = Lists.newArrayList();;
        ArrayList<String> rowList = Lists.newArrayList();
        List caNumberList = Lists.newArrayList();
        Map map = Maps.newHashMap();
        for(int i =0;i<dataList.size();i++){
            rowList =dataList.get(i);
            for(int j =0;j<rowList.size();j++){
                getErrList(dataValidation, errStr, rowList, caNumberList, map, i, j);
            }
        }
        /**判断设备编码是否有重复*/
        if (caNumberList.size() > 0) {
            HashSet set = new HashSet<>(caNumberList);
            if (set.size() != caNumberList.size()) {
                errStr.add( "文件卡口编码有重复!");
            }
        }
        return String.join(";",errStr);
    }

    /**
     * 获取错误信息
     * @param dataValidation    校验公共类
     * @param errStr            错误信息
     * @param rowList           行数信息
     * @param caNumberList      编码集合
     * @param map               查询条件
     * @param i                 行数
     * @param j                 列数
     * @return
     */
    private List<String> getErrList(DataValidation dataValidation, List<String> errStr, ArrayList<String> rowList, List caNumberList, Map map, int i, int j) {
        switch (j){
            /**第一列，卡口编码*/
            case 0:
                if(StringUtils.isNotBlank(rowList.get(j).toString())){
                    if (!dataValidation.chkLength(rowList.get(j).toString().trim(), 0, 20)) {
                        errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,卡口编码数据格式有误");
                    }else {
                        map.put("state", ConstDefine.STATE_ABLE);
                        map.put("baNumber",rowList.get(0));
                        BayonetModel bayonet =bayonetMapper.selectBayonetByBaNumber(map);
                        if (bayonet != null) {
                            errStr.add( "第" + ( i+ 3) + "行卡口编码已存在!");
                        } else {
    //                            保存每一行的设备编码，用来判断是否有重复
                            caNumberList.add(rowList.get(j));
                        }
                    }
                }else {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,卡口编码为必填项");
                }
                break;
            case 1:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,卡口名称为必填项");
                }else if (StringUtils.isNotBlank(rowList.get(j).toString()) &&!dataValidation.chkLength(rowList.get(j).toString().trim(), 0, 60)) {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,卡口名称数据格式有误");
                }
                break;
            case 2:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,卡口类型为必填项");
                }else if (StringUtils.isNotBlank(rowList.get(j).toString()) && !BayonetTypeEnum.isHas(rowList.get(j).toString())) {
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,卡口类型不存在");
                }
                break;
            case 3:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,所属分组为必填项");
                }
                if(StringUtils.isNotBlank(rowList.get(j).toString()) ){
                    map.put("state", ConstDefine.STATE_ABLE);
                    map.put("gName",rowList.get(j).toString());
                    if(bayonetMapper.selectGroupByGName(map) ==null){
                        errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,所属分组不存在");
                    }
                }
                break;
            case 4:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,车道数为必填项");
                }else if (!dataValidation.chekeInteger(subZeroAndDot(rowList.get(j).toString().trim()))) {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,车道数请存入整数");
                }
                break;
            case 5:
                if (StringUtils.isNotBlank(rowList.get(j).toString())&& !OnlineEnum.isHas(rowList.get(j).toString()) ) {
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,连接状态不存在");
                }
                break;
            case 6:
            case 7:
                if (StringUtils.isNotBlank(rowList.get(j).toString()) && !dataValidation.chkLength(rowList.get(j).toString().trim(), 0, 26)) {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,经纬度数据格式有误");
                }
                break;
            case 8:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,车道号为必填项");
                }else if ( !dataValidation.chkLength(rowList.get(j).toString().trim(), 0, 11)) {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,车道号数据格式有误");
                }else if (!dataValidation.chekeZ_index(subZeroAndDot(rowList.get(j).toString().trim()))) {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,车道号请存入正整数");
                }
                break;
            case 9:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,车道类型为必填项");
                }else if (!LaneTypeEnum.isHas(rowList.get(j).toString())) {
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,车道类型不存在");
                }
                break;
            case 10:
                if(StringUtils.isBlank(rowList.get(j).toString())){
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,进出城类型为必填项");
                }else if (!IntoOutEnum.isHas(rowList.get(j).toString())) {
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,进出城类型不存在");
                }
                break;
            case 11:
                if ( (StringUtils.isNotBlank(rowList.get(j).toString()) && (!DirectionEnum.isHas(rowList.get(j).toString())) )) {
                    errStr.add(  "第" +(i+3)+"行" + (j + 1) + "列,行驶方向类型不存在");
                }
                break;
            default:
                if (!dataValidation.chkLength(rowList.get(j).toString().trim(), 0, 11)) {
                    errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,数据数据格式有误,无法保存");
                }


                if(j==12 || j==13){
                    if (!subZeroAndDot(rowList.get(j).toString().trim()).equals("")) {
                        if (!dataValidation.chekeInteger(subZeroAndDot(rowList.get(j).toString().trim()))) {
                            errStr.add( "第" +(i+3)+"行" + (j + 1) + "列,限速请存入整数");
                        }
                    }

                }
                break;
        }
        return errStr;
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     * @param s
     * @return
     */
    private String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }


}
